package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dao.DeviceBoxMapper;
import com.dao.DeviceReplenishRecordMapper;
import com.dao.SysUserMapper;
import com.dao.VenderDeviceMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.DeviceBox;
import com.model.DeviceReplenishRecord;
import com.model.SysUser;
import com.model.VenderDevice;
import com.page.GenericPage;
import com.po.DeviceReplenishRecordPO;
import com.result.Result;
import com.service.ReplenishProductRecordService;
import com.utils.CurrentLoginUserAgent;
import com.utils.NotNullBeanUtils;
import com.vo.DeviceReplenishRecordVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.collections.Lists;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: chihaojie
 * @Date: 2019/7/31 11:08
 * @Version 1.0
 * @Note
 */
@Service
public class ReplenishProductRecordServiceImpl implements ReplenishProductRecordService {

    @Autowired
    private DeviceReplenishRecordMapper deviceReplenishRecordMapper;

    @Autowired
    private VenderDeviceMapper  deviceMapper;

    @Autowired
    private DeviceBoxMapper   deviceBoxMapper;

    @Autowired
    private SysUserMapper    sysUserMapper;


    @Override
    public Result<GenericPage<DeviceReplenishRecordVO>> queryReplenishRecordPage(DeviceReplenishRecordPO replenishRecordPO) {
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        Result<GenericPage<DeviceReplenishRecordVO>> result=new   Result<GenericPage<DeviceReplenishRecordVO>>();
        DeviceReplenishRecord  condition=new DeviceReplenishRecord();
        NotNullBeanUtils.copyNotNullProperties(replenishRecordPO,condition);
        condition.setAgent(businessId);
        final List<DeviceReplenishRecordVO> list= Lists.newArrayList();
        PageInfo<DeviceReplenishRecordVO> pageInfo = PageHelper.startPage(replenishRecordPO.getPageNo(), replenishRecordPO.getPageSize()).doSelectPageInfo(() -> {
            List<DeviceReplenishRecord> replenishRecordList = deviceReplenishRecordMapper.selectByCondition(condition);
            System.out.println(JSONObject.toJSONString(replenishRecordList));
            Optional.ofNullable(replenishRecordList).ifPresent(e -> {
                List<DeviceReplenishRecordVO> collect = replenishRecordList.stream().map(r -> {
                    DeviceReplenishRecordVO vo = new DeviceReplenishRecordVO();
                    BeanUtils.copyProperties(r, vo);
                    SysUser agent = sysUserMapper.selectByPrimaryKey(vo.getAgent());
                    VenderDevice venderDevice = deviceMapper.selectByPrimaryKey(vo.getDeviceId());
                    DeviceBox deviceBox = deviceBoxMapper.selectByPrimaryKey(vo.getBoxId());
                    Optional.ofNullable(venderDevice).ifPresent(d->{ vo.setDeviceName(venderDevice.getDeviceName());});
                    Optional.ofNullable(deviceBox).ifPresent(db->{vo.setBoxType(deviceBox.getBoxType());});
                    Optional.ofNullable(agent).ifPresent(a->{vo.setAgentName(agent.getUserName());});
                    list.add(vo);
                    return vo;
                }).collect(Collectors.toList());
            });
        });
        GenericPage<DeviceReplenishRecordVO> genericPage = new GenericPage<DeviceReplenishRecordVO>(replenishRecordPO.getPageSize(), pageInfo.getTotal(), list);
        return result.ok(genericPage);
    }
}
